package com.example.qwen02.test;

import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.exception.UploadFileException;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/conversation")
@CrossOrigin(origins = "http://localhost:5173")
public class ConversationController {

    private final ConversationService conversationService;

    @Autowired
    public ConversationController(ConversationService conversationService) {
        this.conversationService = conversationService;
    }

    @PostMapping("/generateDescription")
    public String generateDescription(@RequestBody Map<String, String> request) {
        String imageUrl = request.get("imageUrl");
        try {
            return conversationService.generateDescription(imageUrl);
        } catch (ApiException | NoApiKeyException | UploadFileException | JsonProcessingException e) {
            return "{\"error\": \"" + e.getMessage() + "\"}";
        }
    }
}
